from typing import List

from fastapi import APIRouter, HTTPException
from fastapi.params import Body, Depends
from pydantic import BaseModel
from sqlalchemy.orm.session import Session

from common.sqlalchemy_db import SqlalchemyX
from modules.http_resp import ResponseSuccess
from service.GroupService import GroupService

router = APIRouter()


class CreateGroupRequest(BaseModel):
    groupName: str
    groupManagerId: str
    groupType: int

class GroupResponse(BaseModel):
    id: str
    name: str
    managerId: str
    type: int


service = GroupService()

# 创建群聊
@router.post("/group")
async def create_group(request: CreateGroupRequest):
    try:
        group = await service.create_group(request.groupName, request.groupManagerId, request.groupType)
        return ResponseSuccess(group)
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Failed to create group: {str(e)}")

@router.get("/groupList")
async def get_group_list(userId: str):
    async with  SqlalchemyX.AsyncSessionLocal() as session:
        try:
            return await service.get_group_list(session, userId)
        except Exception as e:
            raise HTTPException(status_code=500, detail=f"Failed to fetch group list: {str(e)}")

@router.get("/groupMembers")
async def get_group_members( groupId: str):
    return await service.get_group_members(groupId)



